from pyecharts.charts import Bar
from pyecharts.options import TitleOpts
from pyecharts.render import make_snapshot
# pip inatall snapshot_selenium
from snapshot_selenium import snapshot
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker

# bar_chart = Bar()
# bar_chart.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
# bar_chart.add_yaxis('一季度', [5, 20, 36, 10, 10, 20])
# bar_chart.add_yaxis('二季度', [15, 21, 13, 15, 9, 25])
# bar_chart.add_yaxis('三季度', [16, 24, 33, 8, 9, 17])
# bar_chart.add_yaxis('四季度', [15, 30, 9, 17, 18, 5])
# bar_chart.render()

# make_snapshot(snapshot, bar_chart.render(), 'sales.png')

from partial__01 import create_connect
conn = create_connect(database='hrs')
try:
    with conn.cursor() as cursor:
        cursor.execute('select `dname`, `total` from vi_dept_emp_count')
        # x_data, y_data = [], []
        # # 抓到的是元组
        # for dname, total, in cursor.fetchall():
        #     x_data.append(dname)
        #     y_data.append(total)
        # bar_chart = Bar()
        # title_options = TitleOpts(
        #     title='部门人数统计',
        #     pos_left='65',
        # )
        # # 全局选项
        # bar_chart.set_global_opts(title_opts=title_options)
        # bar_chart.add_xaxis(x_data)
        # bar_chart.add_yaxis('人数', y_data)
        # bar_chart.render()

        pie_char = Pie()
        pie_char.add('', cursor.fetchall())
        pie_char.set_global_opts(
            title_opts=opts.TitleOpts(title="部门人数占比")
        )
        pie_char.set_series_opts(
            label_opts=opts.LabelOpts(formatter="{b},{c},{d}%")
        )
        pie_char.render()
finally:
    conn.close()
# 绘图